package xm.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	/**
	 * @author winnie
	 */	
	public class MovieClipUtil
	{
		
		
		
		private static var _dicComplete: Dictionary	= new Dictionary( true );
		private static var _dicUpdate: Dictionary	= new Dictionary( true );
		
		/**
		 * 제귀함수를 통해 인스턴스명의 접두어를 포함한다면 gotoAndPlay( 1) 을 걸음 
		 * @param namePrePix
		 * 
		 */		
		public static function runMovieClipAndAllChildren( target: MovieClip, namePrePix: String ): void
		{
			var _len: int = target.numChildren;
			for( var i:int=0; i<_len; i++ )
			{
				var _child: MovieClip	= target.getChildAt( i ) as MovieClip;
				if( _child != null && _child.name.indexOf( namePrePix ) == 0 )
				{
					_child.gotoAndPlay( 1 );
					MovieClipUtil.runMovieClipAndAllChildren( _child, namePrePix );
				}
			}
					
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// frame 컨트롤
		//
		//-----------------------------------------------------------------------------------
		
		private static var _dicFrame: Dictionary	= new Dictionary( true );
		
		
		/**
		 * 대상 무비클립을 해당 프레임으로 이동 
		 * @param target
		 * @param frame
		 */		
		public static function moveFrame(target: MovieClip, frame:int, completeHandler: Function = null, updateHandler: Function = null ):void
		{
			_dicFrame[ target ] 	= frame;
			if( completeHandler != null ) _dicComplete[ target ] 	= completeHandler;
			if( updateHandler != null ) _dicUpdate[ target ] 	= updateHandler;
			target.removeEventListener(Event.ENTER_FRAME,onMoveFrameEnter);
			target.addEventListener(Event.ENTER_FRAME,onMoveFrameEnter);
		}
		
		private static function onMoveFrameEnter(e:Event):void
		{
			
			var target: MovieClip 		= e.currentTarget as MovieClip;
			var frame: int 			= _dicFrame[ target ];
			
			
			var updateHandler : Function  = _dicUpdate[ target ];
			if( updateHandler != null ) 
			{
				updateHandler();
			}
			
			if(target.currentFrame>frame){
				target.prevFrame();
			}else if(target.currentFrame<frame){
				target.nextFrame();
			}else{
				target.stop();
				target.removeEventListener(Event.ENTER_FRAME,onMoveFrameEnter);
				var completeHandler: Function = _dicComplete[ target ];
				if( completeHandler != null ) 
				{
					completeHandler();
					delete _dicComplete[ target ];
				}
				
				if( updateHandler != null )
				{
					delete _dicUpdate[ target ];
				}
				
				delete _dicFrame[ target ];
			}
		}
		
		public static function removeMoveFrame( target: MovieClip ):void
		{
			if( _dicFrame[ target ] != null ) delete _dicFrame[ target ];
			if( _dicComplete[ target ] != null ) delete _dicComplete[ target ];
			if( _dicUpdate[ target ] != null ) delete _dicUpdate[ target ];
			
			target.removeEventListener(Event.ENTER_FRAME,onMoveFrameEnter);
		}
		
		
		
		
		private static var arrMcList : Array = [];
		
		/**
		 * 무비클립 오버 아웃 효과만 있는 심플 버튼 만들기
		 */		
		public static function simpleMCButton( mc:MovieClip ):void
		{
			arrMcList.push( mc );
			
			mc.stop();
			mc.buttonMode = true;
			mc.addEventListener(MouseEvent.ROLL_OVER, onObjOver );
			mc.addEventListener(MouseEvent.ROLL_OUT, onObjOut );
			
			function onObjOver( e:MouseEvent ):void
			{
				moveFrame( e.currentTarget as MovieClip, e.currentTarget.totalFrames );
			}
			
			function onObjOut( e:MouseEvent ):void
			{
				moveFrame( e.currentTarget as MovieClip, 1 );
			}
			
		}
		
		
		/**
		 * @param simpleButton 리스트에서 해당 MC 제거
		 */		
		public static function removeSimpleMcButton( mc:MovieClip ):void
		{
			var len:int = arrMcList.length;
			for (var i:int = 0; i < len; i++) 
			{
				if( mc == arrMcList[ i ] as MovieClip )
				{
					delete( arrMcList.splice( arrMcList.indexOf( mc ) , 1 ) )
				}
			}
		}
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		
		public function MovieClipUtil() {}
	}
}